home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / bin / graffiti / lang.c < prev    next >
C/C++ Source or Header  |  1993-01-28  |  6KB  |  314 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include "3d.h"
  4. #include "geom.h"
  5. #include "vect.h"
  6. #include "color.h"
  7. #include "graffiti.h"
  8. #include "lang.h"
  9. #include "handleP.h"
  10. #include "ooglutil.h"
  11. #include "pickfunc.h"
  12.  
  13. static Lake *lake;
  14.  
  15. void handle_pick(char *name, HPoint3 *got, 
  16.          int vert, HPoint3 *v, int gotpoint);
  17. void defhead(float size);
  18. void deftail(float size);
  19. void EraseHeadTail(char *xformname);
  20.  
  21. FILE *outf = NULL;
  22.  
  23. static char *tailgeom = "\
  24. OFF\n\
  25. 8 6 12\n\
  26. -1 -1 -1  # 0\n\
  27.  1 -1 -1  # 1\n\
  28.  1  1 -1  # 2\n\
  29. -1  1 -1  # 3\n\
  30. -1 -1  1  # 4\n\
  31.  1 -1  1  # 5\n\
  32.  1  1  1  # 6\n\
  33. -1  1  1  # 7\n\
  34. 4 0 1 2 3\n\
  35. 4 4 5 6 7\n\
  36. 4 2 3 7 6\n\
  37. 4 0 1 5 4\n\
  38. 4 0 4 7 3\n\
  39. 4 1 2 6 5\n";
  40.  
  41. static char *headgeom = "\
  42. OFF\n\
  43. 5 5 8\n\
  44. -1 -1 -1   # 0\n\
  45.  1 -1 -1   # 1\n\
  46.  1  1 -1   # 2\n\
  47. -1  1 -1   # 3\n\
  48.  0  0  1   # 4\n\
  49. 3 0 1 4\n\
  50. 3 1 2 4\n\
  51. 3 2 3 4\n\
  52. 3 3 0 4\n\
  53. 4 0 1 2 3\n";
  54.  
  55.  
  56. void
  57. handle_pick(char *name, HPoint3 *got, int vert, HPoint3 *v, int gotpoint)
  58. {
  59.   Point3 new;
  60.   HPoint3 *use;
  61.   static int nopick = 0;
  62.   static int notified = 0;
  63.   
  64.   if (gotpoint) {
  65.     if (strcmp(name, tailname)==0) {
  66.       /* user picked the pen's tail --- close up this loop */
  67.       Close();
  68.       NewLine();
  69.     } else {
  70.       use = got;
  71.       if (onlyverts) {
  72.     if (vert)
  73.       use = v;
  74.     else 
  75.       use = NULL;
  76.       }
  77.       if (use) {
  78.     HPt3ToPt3(use, &new);
  79.     AddVertex(&new);
  80.       }
  81.     }
  82.     nopick = 0;
  83.   } else {
  84.     /* Nothing was picked. Keep track of the number of times this
  85.        happens; the first time it happens 3 times in a row, pop
  86.        up a message panel telling the user that they must pick
  87.        on an object in the geomview window. */
  88.     if (!notified)
  89.       if (++nopick >= 3) {
  90.     DisplayPickInfoPanel();
  91.     notified = 1;
  92.       }
  93.   }
  94. }
  95.  
  96.  
  97. /* This macro is defined in pickfunc.h. It does the LDEFINE/LDECLARE for us */
  98.  
  99. DEFPICKFUNC("(pick COORDSYS GEOMID G V E F P VI EI FI)",
  100.         coordsys,
  101.         id,
  102.         point, pn,
  103.         vertex, vn,
  104.         edge, en,
  105.         face, fn, 10,
  106.         ppath, ppn, 50,
  107.         vi,
  108.         ei, ein,
  109.         fi,
  110.         {
  111.           handle_pick(id, &point, vn, &vertex, pn);
  112.           return Lt;
  113.         }
  114. )
  115.      
  116.  
  117.  
  118. void
  119. LangInit(FILE *fp)
  120. {
  121.   lake = LakeDefine(stdin, stdout, NULL);
  122.  
  123.   LInit();
  124.   LDefun("pick", Lpick, Hpick);
  125.  
  126.   outf = fp;
  127.  
  128.   /*
  129.     We could do just "(interest (pick))" which would give us all
  130.     info, in world coordinates, about all picks.  We do the following
  131.     instead to save bandwidth (the nil's prevent the corresponding
  132.     arguments from being passed back), since all we care about is 
  133.     the picked point and whether a it's a vertex.
  134.   */
  135.   progn();
  136.   fprintf(outf, "(interest (pick world * * * nil nil nil nil nil))\n");
  137.   defhead(0.02);
  138.   deftail(0.02);
  139.   endprogn();
  140. }
  141.  
  142.  
  143. void
  144. Input()
  145. {
  146.   LObject *lit, *val;
  147.   lit = LSexpr(lake);
  148.   val = LEval(lit);
  149.   LFree(lit);
  150.   LFree(val);
  151. }
  152.  
  153. Geometry()
  154. {
  155.   Geom *g;
  156.   short dummy = 0;
  157.   short vectc;
  158.  
  159. /*
  160.        CR_NVECT,    # polylines
  161.        CR_VECTC,    # verts in each polyline; array of NVECT shorts
  162.        CR_COLRC,    # colors in each polyline; array of NVECT shorts
  163.        CR_NVERT,    total # vertices
  164.        CR_POINT,    the vertices
  165.        CR_NCOLR,    total # colors
  166.        CR_COLOR,    the colors
  167. */
  168.  
  169.   vectc = (wrap) ? -nverts : nverts;
  170.   g = GeomCreate("vect",
  171.          CR_NVECT,    (nverts) ? 1 : 0,
  172.          CR_NVERT,    nverts,
  173.          CR_VECTC,    &vectc,
  174.          CR_POINT,    verts,
  175.          CR_NCOLR,    0,
  176.          CR_COLRC,    &dummy,
  177.          CR_END);
  178.   fprintf(outf, "(geometry %s { ", graffitiname);
  179.   GeomFSave(g, outf, "graffiti output pipe");
  180.   GeomDelete(g);
  181.   fprintf(outf, " } )\n");
  182.   if (newvect) {
  183.     fprintf(outf, "(normalization %s none)\n", graffitiname);
  184.     fprintf(outf, "(bbox-draw %s off)\n", graffitiname);
  185.     newvect = 0;
  186.   }
  187.   fflush(outf);
  188. }
  189.  
  190. void
  191. defheadtail(char *name, char *xformname, char *geom, float size)
  192. {
  193.   fprintf(outf,"\
  194. (geometry \"%s\" {\n\
  195.     appearance { +edge material { diffuse 0 1 0 } }\n\
  196.     INST transforms :%s\n\
  197.         geom\n\
  198.     { INST\n\
  199.       transform %f 0 0 0\n\
  200.             0 %f 0 0\n\
  201.             0 0 %f 0\n\
  202.             0 0 0  1\n\
  203.       geom { \n", name, xformname, size, size, size);
  204.   fprintf(outf,"%s", geom);
  205.   fprintf(outf,"} } } )\n");
  206.   fprintf(outf,"(normalization %s none)\n", name);
  207.   fprintf(outf,"(bbox-draw %s off)\n", name);
  208.   EraseHeadTail(xformname);
  209. }
  210.  
  211. void
  212. defhead(float size)
  213. {
  214.   progn();
  215.   defheadtail(headname, headxformname, headgeom, size);
  216.   fprintf(outf, "(pickable %s no)\n", headname);
  217.   endprogn();
  218. }
  219.  
  220. void
  221. deftail(float size)
  222. {
  223.   progn();
  224.   defheadtail(tailname, tailxformname, tailgeom, size);
  225.   endprogn();
  226. }
  227.  
  228. void
  229. ShowTailAt(Point3 *p)
  230. {
  231.   fprintf(outf, "(read geometry { define %s ", tailxformname);
  232.   fprintf(outf, "{ TLIST 1 0 0 0  0 1 0 0  0 0 1 0  %f %f %f 1 } } )\n",
  233.       p->x, p->y, p->z);
  234. }
  235.  
  236. void
  237. ShowHeadAt(Point3 *p, Point3 *prev)
  238. {
  239.   Point3 dir;
  240.   Transform R,T,S;
  241.  
  242.   Pt3Sub(p, prev, &dir);
  243.   if (Pt3Length(&dir) == 0.0) {
  244.     dir.x = dir.y = 0;
  245.     dir.z = 1;
  246.   } else {
  247.     Pt3Unit(&dir);
  248.   }
  249.  
  250.   TmRotateTowardZ(R, &dir);
  251.   TmInvert(R,R);
  252.   TmTranslate(T, p->x, p->y, p->z);
  253.   TmConcat(R, T, S);
  254.  
  255.   fprintf(outf, "(read geometry { define %s { TLIST\n", headxformname);
  256.   fputtransform(outf, 1, (float *)S, 0);
  257.   fprintf(outf, "} } )\n");
  258. }
  259.  
  260. void
  261. EraseHeadTail(char *xformname)
  262. {
  263.   fprintf(outf,  "(read geometry { define %s { LIST } } )\n", xformname);
  264. }
  265.  
  266. void EraseHead()
  267. {
  268.   EraseHeadTail(headxformname);
  269. }
  270.  
  271. void EraseTail()
  272. {
  273.   EraseHeadTail(tailxformname);
  274. }
  275.  
  276. void EraseHeadAndTail()
  277. {
  278.   progn();
  279.   EraseHead();
  280.   EraseTail();
  281.   endprogn();
  282. }
  283.  
  284. void DeleteHeadAndTail()
  285. {
  286.   progn();
  287.   fprintf(outf, "(delete %s)\n", headname);
  288.   fprintf(outf, "(delete %s)\n", tailname);
  289.   endprogn();
  290. }
  291.  
  292. /* Rename the old vector (so that different vectors can have different
  293.    appearances) */
  294.  
  295. void StartNewVector()
  296. {
  297.   static int which = 0;
  298.   progn();
  299.   fprintf(outf, "(name-object graffiti graffiti%d)", ++which);
  300.   endprogn();
  301. }
  302.  
  303. progn()
  304. {
  305.   fprintf(outf, "(progn\n");
  306. }
  307.  
  308. endprogn()
  309. {
  310.   fprintf(outf,")\n");
  311.   fflush(outf);
  312. }
  313.  
  314.